﻿/*
 * @author: wizardc
 */

using Unity.Mathematics;

namespace Dou.GPU
{
    /// <summary>
    /// GPU 动画对象
    /// </summary>
    public partial class GPUAnimation
    {
        public GPUAnimationData data;

        private GPUAnimData _animData;
        private string _animName;
        private GPUAnimClipData _animClip;
        
        public GPUAnimation(GPUAnimData animData)
        {
            _animData = animData;
            data.timeScale = 1;
        }
        
        public void Play(string animName, float time = 0)
        {
            if (_animName == animName)
            {
                return;
            }
            _animName = animName;
            for (int i = 0; i < _animData.clips.Length; i++)
            {
                var clip = _animData.clips[i];
                if (clip.name == _animName)
                {
                    _animClip = clip;
                    break;
                }
            }
            data.startIndex = _animClip.startIndex;
            data.frameCount = _animClip.frameCount;
            data.frameTime = _animClip.frameTime;
            data.isPlaying = true;
            data.playTime = time;
            data.totalTime = _animClip.frameTime * _animClip.frameCount;
            data.isLoop = _animClip.isLoop;
        }
        
        public void SetIsLoop(bool isLoop)
        {
            data.isLoop = isLoop;
        }

        public void SetTimeScale(float timeScale)
        {
            data.timeScale = timeScale;
        }

        public void Pause()
        {
            data.isPlaying = false;
        }

        public void Resume()
        {
            data.isPlaying = true;
        }

        public void Stop()
        {
            data.isPlaying = false;
            data.playTime = 0;
        }
    }

    /// <summary>
    /// GPU 动画数据
    /// </summary>
    public struct GPUAnimationData
    {
        public float3 position;
        public quaternion rotation;
        public float3 scale;
        
        public int startIndex;
        public int frameCount;
        public float frameTime;
        
        public bool isPlaying;
        public float playTime;
        public float totalTime;
        public bool isLoop;
        public float timeScale;
        
        public int playIndex;
    }
}
